<template>
  <el-row justify="end">
    <el-button @click="openForm">
      <Icon class="mr-5px" icon="ep:edit" />
      {{ insurance.id ? '编辑保险信息' : '新增保险信息' }}
    </el-button>
  </el-row>
  <label class="font-bold">保险信息</label>
  <InsuranceInfoList :key="insuranceInfoRefKey" ref="insuranceInfoListRef" :insurance="insurance" />
  <ContentWrap>
    <el-collapse v-model="activeNames" class="">
      <el-collapse-item name="systemInfo">
        <template #title>
          <span class="text-base font-bold">系统信息</span>
        </template>
        <el-descriptions :column="4">
          <el-descriptions-item label="创建人">{{ insurance.creatorName }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">
            {{ formatDate(insurance.createTime) }}
          </el-descriptions-item>
          <el-descriptions-item label="修改人">{{ insurance.updaterName }}</el-descriptions-item>
          <el-descriptions-item label="修改时间">
            {{ formatDate(insurance.updateTime) }}
          </el-descriptions-item>
        </el-descriptions>
      </el-collapse-item>
    </el-collapse>
  </ContentWrap>
  <!-- 表单弹窗：添加 -->
  <InsuranceForm ref="formRef" @success="refresh" />
  
</template>
<script lang="ts" setup>
import { InsuranceApi,InsuranceVO } from '@/api/crm/insurance'
import InsuranceInfoList from '@/views/crm/insurance/components/InsuranceInfoList.vue'
import InsuranceForm from '@/views/crm/insurance/InsuranceForm.vue'
import { formatDate } from '@/utils/formatTime'

defineOptions({ name: 'InsuranceInfoDetail' })
const props = defineProps<{ id?: number,bizId?: number }>()
const route = useRoute();
const formRef= ref()
const insuranceInfoRefKey = ref(new Date().getTime())
const formLoading = ref(false) // 表单的加载中
const insurance = ref<InsuranceVO>({} as InsuranceVO) // 保险信息
const openForm = () => {
  formRef.value.open(insurance.value.id ? 'update' : 'create', insurance.value.id, props.bizId)
}
/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.bizId,
  async (val) => {
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      const data = await InsuranceApi.getInsuranceByCustomerId(val)
      if (!data) {
        return
      }
      insurance.value = data
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)
const activeNames = ref(['basicInfo', 'systemInfo']) // 展示的折叠面板
const refresh = async () => {
  insuranceInfoRefKey.value = new Date().getTime()
  try {
    formLoading.value = true
    const data = await InsuranceApi.getInsuranceByCustomerId(props.bizId)
    if (!data) {
      return
    }
    insurance.value = data
  } finally {
    formLoading.value = false
  }
}

/** 初始化 */
onMounted(async () => {
  const id = props.id || route.params.id
  const customerId = props.bizId
  try {
    formLoading.value = true
    if(customerId){
      const data = await InsuranceApi.getInsuranceByCustomerId(customerId)
      if (!data) {
        return
      }
      insurance.value = data
    } else {
      if(id){
        const data = await InsuranceApi.getInsurance(id)
        if (!data) {
          return
        }
        insurance.value = data
      }
    }
  } finally {
    formLoading.value = false
  }
})
</script>
